<template>
  <div class="register">
    <div class="register-container">
      <el-form :model="form" :rules="rules" ref="registerForm" class="register-form">
        <h2 class="title">加入社团联盟</h2>
        <p class="subtitle">创建账户，开启精彩的社团生活</p>
        
        <div class="form-row">
          <el-form-item prop="userName">
            <el-input 
              v-model="form.userName" 
              placeholder="用户名"
              size="large"
              auto-complete="off"
            >
              <template #prefix>
                <el-icon class="input-icon"><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="nickName">
            <el-input 
              v-model="form.nickName" 
              placeholder="昵称"
              size="large"
              auto-complete="off"
            >
              <template #prefix>
                <el-icon class="input-icon"><Avatar /></el-icon>
              </template>
            </el-input>
          </el-form-item>
        </div>
        
        <el-form-item prop="email">
          <el-input 
            v-model="form.email" 
            placeholder="邮箱地址"
            size="large"
            auto-complete="off" 
            type="email"
          >
            <template #prefix>
              <el-icon class="input-icon"><Message /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="phonenumber">
          <el-input 
            v-model="form.phonenumber" 
            placeholder="手机号码"
            size="large"
            auto-complete="off" 
            type="phone"
          >
            <template #prefix>
              <el-icon class="input-icon"><Phone /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <div class="form-row">
          <el-form-item prop="password">
            <el-input 
              v-model="form.password" 
              type="password" 
              placeholder="密码"
              size="large"
              auto-complete="off"
              show-password
            >
              <template #prefix>
                <el-icon class="input-icon"><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input 
              v-model="form.confirmPassword" 
              type="password" 
              placeholder="确认密码"
              size="large"
              auto-complete="off"
              show-password
            >
              <template #prefix>
                <el-icon class="input-icon"><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
        </div>
        
        <el-form-item class="form-actions">
          <el-button 
            type="primary" 
            size="large"
            @click="handleRegister"
            :loading="loading"
            class="register-btn"
          >
            <span v-if="!loading">立即注册</span>
            <span v-else>注册中...</span>
          </el-button>
        </el-form-item>
        
        <div class="form-footer">
          <span>已有账户？</span>
          <el-button type="text" @click="$router.push('/login')" class="link-btn">
            立即登录
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { register } from '@/api/register'
import { ElMessage } from 'element-plus'
import { User, Avatar, Message, Phone, Lock } from '@element-plus/icons-vue'
import router from '@/router'

const registerForm = ref()
const loading = ref(false)
const form = ref({
  userName: '',
  nickName: '',
  password: '',
  confirmPassword: '',
  phonenumber: '',
  email: ''
})

const
rules = {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
          email: [{ required: true, message: "邮箱地址不能为空", trigger: "blur" }, { type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }],
  phonenumber: [{ required: true, message: "手机号码不能为空", trigger: "blur" }, { pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: (rule, value, callback) => {
              if (value !== form.value.password) {
                callback(new Error('两次输入密码不一致'))
              } else {
                callback()
              }
            }, trigger: 'blur' }
        ]
}

const handleRegister = () => {
  loading.value = true
  registerForm.value.validate(valid => {
    if (!valid) {
      loading.value = false
      return
    }
    const data = {
      userName: form.value.userName,
      nickName: form.value.nickName,
      password: form.value.password,
      phonenumber: form.value.phonenumber,
      email: form.value.email
    }
    register(data).then(res => {
      ElMessage.success('注册成功，请登录')
      router.push('/login')
    }).catch(err => {
      ElMessage.error(err.msg || '注册失败')
    }).finally(() => {
      loading.value = false
    })
  })
}
</script>

<style scoped>
.register {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.register-container {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 500px;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.register-form {
  width: 100%;
}

.title {
  text-align: center;
  margin-bottom: 8px;
  color: #2c3e50;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.subtitle {
  text-align: center;
  margin-bottom: 32px;
  color: #7f8c8d;
  font-size: 14px;
  font-weight: 400;
}

.form-row {
  display: flex;
  gap: 16px;
}

.form-row .el-form-item {
  flex: 1;
  margin-bottom: 20px;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input {
  border-radius: 12px;
}

:deep(.el-input__wrapper) {
  border-radius: 12px;
  border: 2px solid #e1e8ed;
  box-shadow: none;
  transition: all 0.3s ease;
  padding: 12px 16px;
}

:deep(.el-input__wrapper:hover) {
  border-color: #667eea;
}

:deep(.el-input__wrapper.is-focus) {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

:deep(.el-input__inner) {
  font-size: 14px;
  color: #2c3e50;
  font-weight: 500;
}

:deep(.el-input__inner::placeholder) {
  color: #bdc3c7;
  font-weight: 400;
}

.input-icon {
  color: #95a5a6;
  font-size: 16px;
}

.form-actions {
  margin-top: 32px;
  margin-bottom: 24px;
}

.register-btn {
  width: 100%;
  height: 48px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
  letter-spacing: 0.5px;
}

.register-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}

.register-btn:active {
  transform: translateY(0);
}

.form-footer {
  text-align: center;
  color: #7f8c8d;
  font-size: 14px;
}

.link-btn {
  color: #667eea;
  font-weight: 600;
  padding: 0;
  margin-left: 8px;
  text-decoration: none;
  transition: color 0.3s ease;
}

.link-btn:hover {
  color: #764ba2;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register {
    padding: 16px;
  }
  
  .register-container {
    padding: 24px;
    max-width: 100%;
  }
  
  .form-row {
    flex-direction: column;
    gap: 0;
  }
  
  .title {
    font-size: 24px;
  }
}

@media (max-width: 480px) {
  .register-container {
    padding: 20px;
    border-radius: 16px;
  }
  
  .title {
    font-size: 22px;
  }
  
  .subtitle {
    font-size: 13px;
  }
}
</style>
